var stompClient = null;
$(function () {
    connect();
    $("#send").click(function () {
        var uname = $("#uname").val();
        var content = $("#content").val();
        if(!uname || !content) {
            alert('请输入用户名|内容');
            return;
        }
        var msg = {"name":uname, "content": content};
        //调用控制器，发送消息：app跟 registry.setApplicationDestinationPrefixes("/app")对应
        //hello @MessageMapping("/hello")对应
        stompClient.send('/app/hello', {}, JSON.stringify(msg));
    });
})

function connect() {
    var socket = new SockJS("/chat");
    stompClient = Stomp.over(socket);
    stompClient.connect({}, function (frame) {
        //回调函数，监听指定位置的消息
        stompClient.subscribe('/topic/greetings', function (greeting) {
            var msg = JSON.parse(greeting.body);//把字符串转换成json对象
            $("#info").append("<div>"+msg.name + "说：" + msg.content +"</div>");
        })
    })
}
